/*
 *  $Id: Text.cs 1231 2010-12-07 12:24:56Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008-2010 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using AgateLib.Geometry;

using Nereal.Serialization;

using Ragima.Helper;

namespace Ragima.UI {
    /// <summary>
    /// Текст: простой виджет для вывода текста.
    /// </summary>
    [TypeId("text")]
    public class Text : Widget {
        private static readonly Color DefaultColor = Color.White;
        private const TextAlignment DefaultAlignment = TextAlignment.Left;
        private const int DefaultPadding = 0;
        private const bool DefaultLocale = false;

        private TextAlignment _alignment;
        private int _padding;
        private bool _locale;
        private string _value;
        private Languages.Phrase _phrase;

        public Text() {
            _alignment = DefaultAlignment;
            _padding = DefaultPadding;
            _locale = DefaultLocale;
            _value = string.Empty;
            _phrase = null;
        }

        /// <summary>
        /// Выравнивание текста.
        /// </summary>
        [XmlName("alignment"), DefaultValue(DefaultAlignment)]
        public TextAlignment Alignment {
            get { return _alignment; }
            set { _alignment = value; }
        }

        /// <summary>
        /// Внутренний отступ текста.
        /// </summary>
        [XmlName("padding"), DefaultValue(DefaultPadding)]
        public int Padding {
            get { return _padding; }
            set { _padding = value; }
        }

        /// <summary>
        /// Является ли этот текст локализованным.
        /// </summary>
        [XmlName("locale"), DefaultValue(DefaultLocale)]
        public bool Locale {
            get { return _locale; }
            set {
                _locale = value;
                _phrase = null;
            }
        }

        /// <summary>
        /// Значение текстового виджета (сам текст или идентификатор локализуемого ресурса).
        /// </summary>
        [XmlName("value"), DefaultValue("")]
        public string Value {
            get { return _value; }
            set {
                _value = value;
                _phrase = null;
            }
        }

        /// <summary>
        /// Отображаемый текст.
        /// </summary>
        public string DisplayValue {
            get {
                if (string.IsNullOrEmpty(_value))
                    return string.Empty;
                if (_locale) {
                    if (_phrase == null)
                        _phrase = new Languages.Phrase(_value);
                    return _phrase.ToString();
                } else {
                    return _value;
                }
            }
        }

        /// <summary>
        /// Рисует текст.
        /// </summary>
        public override void Draw() {
            var font = Font;
            var text = DisplayValue;
            if (font == null || string.IsNullOrEmpty(text))
                return;
            font.Color = MainColor ?? DefaultColor;
            var rect = AbsoluteBounds.Padding(_padding);
            font.Draw(rect, text, _alignment);
        }
    }
}
